//----------------------------------------------------------------------------------
// CRenderableVertexs class
// Author: Oscar Serrano
//
// Description:
// Permite renderizar vertices
//----------------------------------------------------------------------------------

#pragma once

#ifndef RENDERABLE_VERTEXS_H
#define RENDERABLE_VERTEXS_H

#include <d3dx9.h>
#include "RenderManager.h"
#include "Utils\Base.h"
#include "Core\Core.h"
#include "RenderManager.h"
#include <assert.h>
#include "VertexTypes.h"

class CRenderableVertexs
{
protected:
	LPDIRECT3DVERTEXBUFFER9			m_VB;
	LPDIRECT3DINDEXBUFFER9			m_IB;
	size_t							m_IndexCount, m_VertexCount;
public:
	CRenderableVertexs() {}
	virtual ~CRenderableVertexs						()
	{
		CHECKED_RELEASE(m_VB);
		CHECKED_RELEASE(m_IB);
	}
	virtual bool					Render			(CRenderManager *GM)  = 0;
	virtual inline size_t			GetFacesCount	() const
	{
		return m_IndexCount / 3;
	}
	virtual inline size_t			GetVertexsCount	() const
	{
		return m_VertexCount;
	}
	virtual inline unsigned	short	GetVertexType	() = 0;
	virtual inline size_t			GetVertexSize	() = 0;
	virtual inline size_t			GetIndexSize	() = 0;
};

template<class T>
class CIndexedVertexs : public CRenderableVertexs
{
protected:
	inline size_t		GetVertexSize		() {return sizeof(T);}
	inline size_t		GetIndexSize		() {return sizeof(unsigned short);}
public:
	CIndexedVertexs(CRenderManager *RM, void *VertexAddress, void *IndexAddres, size_t	VertexCount, size_t IndexCount)
	{
		m_IndexCount = IndexCount;
		m_VertexCount = VertexCount;
		if( FAILED( CORE->GetRenderManager()->GetDevice()->CreateVertexBuffer( m_VertexCount*GetVertexSize(),
			0 /*Usage*/, T::GetFVF(), D3DPOOL_DEFAULT, &m_VB, NULL ) ) )
			assert(E_FAIL);


		VOID* pVertices;

		if(FAILED(m_VB->Lock(0, GetVertexSize()*m_VertexCount, 
			(VOID**)&pVertices, 0 ) ) ) 
			assert(E_FAIL);

		memcpy(pVertices, VertexAddress, GetVertexSize()*m_VertexCount);
		m_VB->Unlock();
	
		if( FAILED( CORE->GetRenderManager()->GetDevice()->CreateIndexBuffer( m_IndexCount*GetIndexSize(),
				0 /*Usage*/, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_IB, NULL ) ) )
			assert(E_FAIL);


		if(FAILED(m_IB->Lock(0, GetIndexSize()*m_IndexCount, 
				(VOID**)&pVertices, 0 ) ) ) 
			assert(E_FAIL);

		memcpy(pVertices, IndexAddres, GetIndexSize()*m_IndexCount);
		m_IB->Unlock();

	}

	~CIndexedVertexs()
	{
	}
	virtual bool Render(CRenderManager *RM)
	{
		RM->GetDevice()->SetFVF(T::GetFVF());
		RM->GetDevice()->SetStreamSource(0, m_VB, 0, GetVertexSize());
		RM->GetDevice()->SetIndices(m_IB);
		return (!FAILED (RM->GetDevice()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, GetVertexsCount(), 0, GetFacesCount())	)	);

	}
	virtual inline unsigned short GetVertexType() {return T::GetVertexType();}
};



//CRenderableVertexs *l_RV=new CIndexedVertexs<TCOLORED_VERTEX>(CORE->GetRenderManager(), NULL, NULL, 0, 0);




#endif //RENDERABLE_VERTEXS_H